﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Card.Make.Pro.Application.Helper;

namespace Card.Make.Pro.Form
{
    public partial class Frm_Dialog : DevExpress.XtraEditors.XtraForm
    {
        public Frm_Dialog()
        {
            InitializeComponent();
            this.txtSearch.Edit.KeyDown += new KeyEventHandler(Edit_KeyDown);
        }

        void Edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadingHelper.Open();

                var value = (sender as DevExpress.XtraEditors.TextEdit)?.EditValue;
                txtSearch.EditValue = value;
                Query();
                LoadingHelper.Close();
            }
        }



        private string connStr = "";
        SqlHelper sqlHelper = new SqlHelper();
        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadingHelper.Open();
            Query();
            LoadingHelper.Close();
        }


        private void Query()
        {
            var txtSearchWord = this.txtSearch.EditValue?.ToString();
            if (!string.IsNullOrEmpty(txtSearchWord))
            {
                connStr = " Select [LogIndex],[LogDT],[UserAccount],convert(varchar(10),[LogSource]) as LogSource,convert(varchar(10),[LogType]) as LogType,[ComputerName],[IPAddress],[MAC],[LogDesc] from Log_Table where [LogDesc] like '%" + txtSearchWord + "%' or UserAccount like '%" + txtSearchWord + "%'  order by [LogIndex] DESC";
            }
            else
            {
                connStr = " Select [LogIndex],[LogDT],[UserAccount],convert(varchar(10),[LogSource]) as LogSource,convert(varchar(10),[LogType]) as LogType,[ComputerName],[IPAddress],[MAC],[LogDesc] from Log_Table  order by [LogIndex] DESC";
            }
            BindGridByConn(connStr);
        }

        private void BindGridByConn(string connStr)
        {
            DataTable dt = sqlHelper.GetQueryResult(connStr);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["LogType"].ToString())
                {

                    case "1":
                        dt.Rows[i]["LogType"] = "添加账号";
                        break;
                    case "2":
                        dt.Rows[i]["LogType"] = "修改账号";
                        break;
                    case "3":
                        dt.Rows[i]["LogType"] = "删除账号";
                        break;
                    case "1001":
                        dt.Rows[i]["LogType"] = "添加审批人";
                        break;

                    case "1002":
                        dt.Rows[i]["LogType"] = "修改审批人";
                        break;
                    case "1003":
                        dt.Rows[i]["LogType"] = "删除审批人";
                        break;
                    case "1004":
                        dt.Rows[i]["LogType"] = "添加证卡";
                        break;
                    case "1005":
                        dt.Rows[i]["LogType"] = "更新证卡";
                        break;
                    case "1006":
                        dt.Rows[i]["LogType"] = "删除证卡";
                        break;
                    case "1007":
                        dt.Rows[i]["LogType"] = "添加证卡";
                        break;
                    case "1008":
                        dt.Rows[i]["LogType"] = "更新证卡";
                        break;
                    case "1009":
                        dt.Rows[i]["LogType"] = "删除证卡";
                        break;
                    case "1010":
                        dt.Rows[i]["LogType"] = "还原证卡";
                        break;
                    case "1011":
                        dt.Rows[i]["LogType"] = "清空回收站";
                        break;
                    case "1012":
                        dt.Rows[i]["LogType"] = "证卡加锁";
                        break;
                    case "1013":
                        dt.Rows[i]["LogType"] = "证卡解锁";
                        break;
                    case "1014":
                        dt.Rows[i]["LogType"] = "证卡打印";
                        break;
                    case "1015":
                        dt.Rows[i]["LogType"] = "证卡授权";
                        break;
                    case "1016":
                        dt.Rows[i]["LogType"] = "证卡发放";
                        break;
                    case "1017":
                        dt.Rows[i]["LogType"] = "添加照片库记录";
                        break;
                    case "1018":
                        dt.Rows[i]["LogType"] = "更新照片库记录";
                        break;
                    case "1019":
                        dt.Rows[i]["LogType"] = "删除照片库记录";
                        break;
                    case "1020":
                        dt.Rows[i]["LogType"] = "接受外部数据申请";
                        break;
                    case "1021":
                        dt.Rows[i]["LogType"] = "拒绝外部数据申请";
                        break;
                    case "1022":
                        dt.Rows[i]["LogType"] = "批量删除证卡";
                        break;

                }

                switch (dt.Rows[i]["LogSource"].ToString())
                {
                    case "1":
                        dt.Rows[i]["LogSource"] = "制证管理系统";
                        break;
                    case "2":
                        dt.Rows[i]["LogSource"] = "验证管理系统";
                        break;
                    case "3":
                        dt.Rows[i]["LogSource"] = "综合监控管理系统";
                        break;
                    case "4":
                        dt.Rows[i]["LogSource"] = "值班室访客管理系统";
                        break;
                    case "5":
                        dt.Rows[i]["LogSource"] = "通知管理系统";
                        break;
                    case "6":
                        dt.Rows[i]["LogSource"] = "报到考勤系统";
                        break;
                }

            }
            this.gridControl1.DataSource = dt;
        }
    }
}